<template>
  <div class="index">
    <div class="header">
      <div class="logo">
        <span class="iconfont iconnew"></span>
      </div>
      <div class="search" @click="$router.push({ name: 'search' })">
        <van-icon name="search" />
        <span>搜索新闻</span>
      </div>
      <div class="user" @click="$router.push({ path: `/personal/${userId}` })">
        <van-icon name="manager-o" />
      </div>
    </div>
    <van-tabs v-model="active" swipeable sticky>
      <van-tab v-for="item in categoryList" :title="item.name" :key="item.id">
        <van-list
          v-model="item.loading"
          :finished="item.finished"
          :offset="20"
          :immediate-check="false"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <van-pull-refresh v-model="item.isLoading" @refresh="onRefresh">
            <articleBlock
              v-for="(value, index) in item.postList"
              :key="index"
              :postArticle="value"
            ></articleBlock>
          </van-pull-refresh>
        </van-list>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { getCategory } from "@/apis/category.js";
import { getarticleList } from "@/apis/post.js";
import articleBlock from "@/components/articleBlock.vue";
import { Toast } from "vant";
export default {
  components: {
    articleBlock,
  },
  data() {
    return {
      categoryList: [],
      active: localStorage.getItem("hmtoutiao_token") ? 1 : 0,
      userId: localStorage.getItem("user_id")
        ? localStorage.getItem("user_id")
        : null,
    };
  },
  async mounted() {
    if (localStorage.getItem("hmtoutiao_token")) {
      if (localStorage.getItem("categoryList")) {
        this.categoryList = JSON.parse(localStorage.getItem("categoryList"));
      }
    } else {
      let res = await getCategory();
      this.categoryList = res.data.data;
    }
    this.categoryList = this.categoryList.map((value) => {
      return {
        ...value, // 展开成员进行深拷贝，需要原本的所有成员
        postList: [], // 想要各栏目数据互不干扰，改造源数据，加一个postList数组来存储自己栏目的数据
        pageIndex: 1,
        pageSize: 4,
        loading: false,
        finished: false,
        isLoading: false,
      };
    });
    this.init();

    // 点击伪元素跳转
    document.querySelector(".van-sticky").onclick = (e) => {
      // console.log(e.target.className);
      if (e.target.className == "van-sticky") {
        // console.log("我是伪元素+");
        this.$router.push({ name: "categoryManager" });
      }
    };
  },
  watch: {
    active() {
      // console.log(this.active);
      if (this.categoryList[this.active].postList.length == 0) this.init();
    },
  },
  methods: {
    // 下拉刷新
    onRefresh() {
      this.categoryList[this.active].postList.length = 0;
      this.categoryList[this.active].pageIndex = 1;
      this.categoryList[this.active].finished = false;
      // console.log(this.categoryList[this.active].pageIndex);
      this.init();
    },
    // 下拉加载更多
    onLoad() {
      this.categoryList[this.active].pageIndex++;
      // 模拟延时效果
      // setTimeout(() => {
      //   this.init();
      // }, 1000);
      this.init();
    },
    // 加载对应栏目新闻
    async init() {
      // console.log(this.categoryList[this.active].pageIndex);
      let res2 = await getarticleList({
        category: this.categoryList[this.active].id,
        pageIndex: this.categoryList[this.active].pageIndex,
        pageSize: this.categoryList[this.active].pageSize,
      });
      // console.log("对应栏目新闻数据", res2);
      // 对数据进行改造
      // if (res2.status == 200) {
      this.categoryList[this.active].postList.push(...res2.data.data);
      // }
      // 改变上拉加载更多状态
      this.categoryList[this.active].loading = false;
      // 没数据了就改变finished值
      // let totalPage = parseInt(
      //   res2.data.total / this.categoryList[this.active].pageSize
      // );
      // console.log(totalPage);
      if (res2.data.data.length < this.categoryList[this.active].pageSize) {
        this.categoryList[this.active].finished = true;
      }

      // 获取完成改变下拉loading
      this.categoryList[this.active].isLoading = false;
      // console.log(this.categoryList);
    },
  },
};
</script>

<style lang="less" scoped>
.index {
  .header {
    // width: 100vw;
    // width: 100%;
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #ff0000;
    color: #fff;
    .logo {
      span {
        font-size: 50px;
        padding: 0 10px;
      }
    }
    .search {
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center;
      flex: 1;
      height: 30px;
      line-height: 30px;
      background-color: rgba(255, 255, 255, 0.4);
      border-radius: 15px;
      .van-icon {
        font-size: 18px;
      }
    }
    .van-icon {
      font-size: 24px;
      padding: 0 10px;
    }
  }
}
// 修改vant顶部栏目样式
/deep/ .van-sticky {
  padding-right: 50px;
  &::after {
    content: "+";
    width: 50px;
    height: 44px;
    position: absolute;
    top: 0;
    right: 0;
    line-height: 44px;
    font-size: 32px;
    background-color: #fff;
    text-align: center;
  }
}
</style>